package com.ziven.common.widget.value;

import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;

import com.ziven.easygo.util.ResourceUtils;
import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public final class Background extends Value {

    public static Background drawableId(@DrawableRes int drawableId) {
        return new Background().accept(my -> my.resId = drawableId);
    }

    public static Background colorId(@ColorRes int colorId) {
        return color(ResourceUtils.getColor(colorId));
    }

    public static Background color(@ColorInt int color) {
        return new Background().accept(my -> my.value = color);
    }

    public static Background drawable(Drawable drawable) {
        return new Background().accept(my -> my.value = drawable);
    }

    private Background() {}

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {
        if(resId == 0) {
            if(!value(Integer.class, helper::setBackgroundColor)) {
                value(Drawable.class, helper::setBackground);
            }
        } else {
            helper.setBackgroundResource(resId);
        }
    }
}
